package com.kuzan.autocloud.client.sys.config.service;

import com.kuzan.autocloud.client.sys.helper.HttpRequestHelper;
import com.kuzan.autocloud.client.sys.util.HttpRequestUtil;
import com.kuzan.autocloud.common.model.menu.MenuGroup;
import com.kuzan.autocloud.common.model.menu.MenuRoot;
import com.kuzan.autocloud.common.model.system.DeletionRecord;
import com.kuzan.autocloud.common.repository.menu.MenuGroupRepository;
import com.kuzan.autocloud.common.repository.menu.MenuItemRepository;
import com.kuzan.autocloud.common.repository.menu.MenuRootRepository;
import com.kuzan.autocloud.common.repository.system.ACPermissionRepository;
import com.kuzan.autocloud.common.repository.system.DeletionRecordRepository;
import com.kuzan.autocloud.common.util.GsonUtil;
import com.kuzan.autocloud.common.util.UuidUtil;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.text.Format;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <p>Description: </p>
 *
 * @author kuzank 2019/12/8
 */
@Service
public class CMenuService {

    @Autowired
    MenuRootRepository rootRepository;
    @Autowired
    MenuGroupRepository groupRepository;
    @Autowired
    MenuItemRepository itemRepository;
    @Autowired
    DeletionRecordRepository deletionRecordRepository;
    @Autowired
    ACPermissionRepository acPermissionRepository;
    @Autowired
    HttpServletRequest servletRequest;


    public boolean rootOnDrop(List<String> ids) {
        Format ranksFormat = new DecimalFormat("0000");
        AtomicInteger count = new AtomicInteger(0);
        ids.forEach(id -> {
            Optional<MenuRoot> rootOptional = rootRepository.findById(id);
            if (rootOptional.isPresent()) {
                MenuRoot root = rootOptional.get();
                count.incrementAndGet();
                root.setSort("c" + ranksFormat.format(count.get()));
                rootRepository.save(root);
            }
        });
        return true;
    }

    public MenuRoot rootOnCreate(Map map) {
        if (map == null) {
            return null;
        }
        MenuRoot root = MenuRoot.of(map.get("title").toString(), UuidUtil.randomUUID());
        root.setCreator(HttpRequestUtil.getPeople(servletRequest));
        rootRepository.save(root);
        return root;
    }

    public boolean rootOnDelete(String id) {
        Optional<MenuRoot> rootOptional = rootRepository.findById(id);
        if (rootOptional.isPresent()) {
            DeletionRecord deletionRecord = DeletionRecord.ofCollection("sys_menu_root", HttpRequestUtil.getPeople(servletRequest), GsonUtil.gson.fromJson(GsonUtil.gson.toJson(rootOptional.get()), HashMap.class));
            deletionRecordRepository.save(deletionRecord);
            rootRepository.deleteById(deletionRecord.getId());
            return true;
        } else {
            return false;
        }
    }

    public boolean rootOnEdit(Map map) {
        if (map == null) {
            return false;
        }
        Optional<MenuRoot> rootOptional = rootRepository.findById(String.valueOf(map.get("id")));
        if (rootOptional.isPresent()) {
            MenuRoot root = rootOptional.get();
            root.setTitle(String.valueOf(map.get("title")));
            rootRepository.save(root);
            return true;
        } else {
            return false;
        }
    }

    public boolean groupOnDrop(List<String> ids) {
        Format ranksFormat = new DecimalFormat("0000");
        AtomicInteger count = new AtomicInteger(0);
        ids.forEach(id -> {
            Optional<MenuGroup> groupOptional = groupRepository.findById(id);
            if (groupOptional.isPresent()) {
                MenuGroup group = groupOptional.get();
                count.incrementAndGet();
                group.setSort("c" + ranksFormat.format(count.get()));
                groupRepository.save(group);
            }
        });
        return true;
    }

    public Map getGroupInfoById(String id) {
        Map map = new HashMap();
        map.put("group", groupRepository.findById(id).get());
        map.put("items", groupRepository.findAllByBelongOrderBySortAsc(id));
        map.put("permission", acPermissionRepository.findByResource(id));
        return map;
    }

}
